package io.renren.modules.skate.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.renren.common.utils.image.ImageTool;
import io.renren.modules.nuaa.entity.AllContentEntity;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.skate.dto.FCertificateDTO;
import io.renren.modules.skate.dto.FClassifyDTO;
import io.renren.modules.skate.dto.FCompetitionDTO;
import io.renren.modules.skate.dto.FContestItemDTO;
import io.renren.modules.skate.excel.FCertificateExcel;
import io.renren.modules.skate.excel.FGradeCertificateExcel;
import io.renren.modules.skate.service.FCertificateService;
import io.renren.modules.skate.service.FClassifyService;
import io.renren.modules.skate.service.FCompetitionService;
import io.renren.modules.skate.service.FContestItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Scope("prototype")
public class FCertificateListener extends AnalysisEventListener<FCertificateExcel> {

    @Autowired
    private FCertificateService fCertificateService;

    @Autowired
    private FClassifyService classifyService;

    @Autowired
    private FCompetitionService fCompetitionService;

    @Autowired
    private FContestItemService fContestItemService;

    @Override
    public void invoke(FCertificateExcel fCertificateExcel, AnalysisContext analysisContext) {

        FCertificateDTO fCertificateDTO = new FCertificateDTO();

        /*if(StringUtils.isNotBlank(fCertificateExcel.getCompetitionName())){
            FCompetitionDTO fCompetitionDTO = fCompetitionService.queryByName(fCertificateExcel.getCompetitionName());
            if(fCompetitionDTO != null){
                fCertificateDTO.setCompetitionId(fCompetitionDTO.getId().intValue());
            }
        }
        if(StringUtils.isNotBlank(fCertificateExcel.getContestItemName())){
            FContestItemDTO fContestItemDTO = fContestItemService.queryByName(fCertificateExcel.getContestItemName());
            if(fContestItemDTO != null){
                fCertificateDTO.setContestItemId(fContestItemDTO.getId().intValue());
            }
        }*/
        // 生成证书
//        String certificateImgStr = this.generateCer(fCertificateExcel);
//
//        fCertificateDTO.setImg(certificateImgStr);

        fCertificateDTO.setName(fCertificateExcel.getName());
        fCertificateDTO.setIdCard(fCertificateExcel.getIdCard());
        //fCertificateDTO.setScore(fCertificateExcel.getScore());
        fCertificateDTO.setCreateDate(new Date());
        fCertificateDTO.setCreator(SecurityUser.getUserId().toString());
        fCertificateDTO.setUpdater(SecurityUser.getUserId().toString());
        fCertificateDTO.setUpdateDate(new Date());
        fCertificateDTO.setType(2);

        fCertificateService.save(fCertificateDTO);
    }

    private String generateCer(FCertificateExcel fCertificateExcel){
        AllContentEntity rongyu = ImageTool.rongyu("EDECO-2021-0001",
                fCertificateExcel.getName(),
                "测试大赛",
                "低年级组",
                "一等奖",
                "第一小学",
                "张老师",
                "http://localhost/img/1700460482251.jpg",
                "http://localhost/img/certificategroundback.jpg");
        return ImageTool.drawList(rongyu);
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
